//
//  CommentSectionView.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/16.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "CommentSectionView.h"

@interface CommentSectionView ()
@property(nonatomic, strong)UIButton *headerButton;
@property(nonatomic, strong)UILabel *nickNameLabel;
@property(nonatomic, strong)UIView *centerView;
@property(nonatomic, strong)UILabel *timeLabel;
///赞
@property(nonatomic, strong)UIButton *praiseButton;

@property(nonatomic, strong)UILabel *commentLabel;
///分割线
@property(nonatomic, strong)UIView *lineView;

@end

@implementation CommentSectionView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithReuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.headerButton.clipsToBounds = YES;
        self.headerButton.layer.cornerRadius = 20.0;
        [self addSubview:self.headerButton];
        self.headerButton.backgroundColor = [UIColor xm_MainColor];
        
        self.centerView = [[UIView alloc] init];
        [self addSubview:self.centerView];
        
        self.nickNameLabel = [[UILabel alloc]init];
        self.nickNameLabel.font = [UIFont xm_font:14.0];
        self.nickNameLabel.textColor = [UIColor xm_MainColor];
        [self addSubview:self.nickNameLabel];
        
        self.timeLabel = [[UILabel alloc]init];
        self.timeLabel.font = [UIFont xm_font:12.0];
        self.timeLabel.textColor = [UIColor xm_somberColor];
        [self addSubview:self.timeLabel];
        
        self.praiseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.praiseButton setImage:[UIImage imageNamed:@"image_delete"] forState:UIControlStateNormal];
        [self.praiseButton setTitleColor:[UIColor xm_MainColor] forState:UIControlStateSelected];
        [self.praiseButton setTitleColor:[UIColor xm_somberColor] forState:UIControlStateNormal];
        self.praiseButton.imageEdgeInsets = UIEdgeInsetsMake(0, -4, 0, 0);
        self.praiseButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -4);
        [self.praiseButton addTarget:self action:@selector(praiseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.praiseButton];
        
        self.commentLabel = [[UILabel alloc] init];
        self.commentLabel.numberOfLines = 0;
        self.commentLabel.font = kCommentFont;
        self.commentLabel.textColor = [UIColor xm_jetblackColor];
        [self addSubview:self.commentLabel];
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor xm_splitlineColor];
        [self addSubview:self.lineView];
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer)];
        [self.contentView addGestureRecognizer:tapGesture];
    }
    return self;
}
-(void)praiseButtonClick:(UIButton *)sender{
    NSDictionary * dict = @{@"ArticleID":@(self.comment.ArticleID),
                            @"CommentID":@(self.comment.CommentID),
                            @"SupportOrNo":@(!sender.selected)
                            };
    
    [FBNetworkTool get:@"articleapi/com.SupportOrNoCommentInfo" parameters:dict showHUD:NO success:^(XMHttpResponseModel *responseModel) {
        if (responseModel.code == 200) {
            if (sender.selected) {
                
            }
            sender.selected = !sender.selected;
        }
    } failure:^(NSError *error) {
        
    }];

}
-(void)tapGestureRecognizer{
    if (self.block) {
        self.block(self.comment);
    }
}
-(void)setCommentType:(NSInteger)commentType{
    _commentType = commentType;
}

-(void)setComment:(AticleComment *)comment{
    _comment = comment;
    [self.headerButton xm_setImageWithURL:_comment.Logo forState:UIControlStateNormal placeholderImage:nil];
    self.nickNameLabel.text = _comment.NikeName;
    self.timeLabel.text = [XMTool  timeIntervalFromLastTime:_comment.UpdateTime];
    [self.praiseButton setTitle:[NSString stringWithFormat:@"%ld",(long)_comment.SupportMan] forState:UIControlStateNormal];

    if ([_comment.Content containsString:@"||"]) {
        NSArray * array = [_comment.Content componentsSeparatedByString:@"||"];
        NSString * str1 = array.firstObject;
        NSString * str2 = [NSString stringWithFormat:@"回复%@：",array.lastObject];
        NSString * contentText = [NSString stringWithFormat:@"%@%@",str2,str1];
        NSMutableAttributedString * attributed = [[NSMutableAttributedString alloc] initWithString:contentText];

        [attributed addAttribute:NSForegroundColorAttributeName
                           value:[UIColor xm_somberColor]
                           range:NSMakeRange(0, str2.length)];
        self.commentLabel.attributedText = attributed;
    }else{
        NSMutableAttributedString * attributed = [[NSMutableAttributedString alloc] initWithString:_comment.Content];
        [attributed addAttribute:NSForegroundColorAttributeName
                           value:[UIColor xm_jetblackColor]
                           range:NSMakeRange(0, _comment.Content.length)];
        self.commentLabel.attributedText = attributed;
    }

    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    [self.headerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self).offset(15.0);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.headerButton);
        make.left.mas_equalTo(self.headerButton.mas_right).offset(10);
        make.height.mas_equalTo(@4);
    }];
    
    [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.centerView);
        make.bottom.mas_equalTo(self.centerView.mas_top);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.centerView);
        make.top.mas_equalTo(self.centerView.mas_bottom);
    }];
    
    [self.praiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.centerView);
        make.right.mas_equalTo(self).offset(-15);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.centerView);
        make.right.mas_equalTo(self).offset(-15);
        make.top.mas_equalTo(self.headerButton.mas_bottom).offset(10);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-15);
        make.height.mas_equalTo(@0.5);
    }];
}

@end
